package com.system.config;

import com.system.common.BaseController;
import com.system.common.ResponseCode;
import com.system.entity.vo.RspResultVO;
import io.jsonwebtoken.SignatureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author 佛冷
 * @date 2019/10/11
 */
@RestControllerAdvice
public class ExceptionHandle extends BaseController {

    static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RspResultVO handle(Exception e) {
        e.printStackTrace();
        if (e.getClass() == MethodArgumentNotValidException.class) {
            return returnResult(ResponseCode.PARAM_LACK_ERROR);
        } else if (e.getClass() == SignatureException.class) {
            return returnResult(ResponseCode.UNAUTHORIZED.getCode(),e.getMessage());
        }
        return returnResult(ResponseCode.RESPONSE_ERROR.getCode(), ResponseCode.RESPONSE_ERROR.getMsg(), e.getMessage());
    }
}
